<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/23
 * Time: 17:29
 * 权限控制
 */

namespace app\admin\controller;
use app\Base as parentBase;
use app\common\model\NodeModel;
use think\Controller;
use think\Db;
use util\Auth;
use think\Request;

class Base extends parentBase
{
    public function __construct (){
        parent::__construct();


        $admin_info = session("admin_info");
//        dd($admin_info);
        if(!$admin_info){
           $this->error('非法登录！！');
        }else{

            define('UID', $admin_info['id']);
            $auth = new Auth();
            $request =  Request::instance();
            $url =  $request->module().'/'.$request->controller().'/'.$request->action();
            $not_check = array('admin/Index/index','admin/Index/indexpage');
           /* //记录日志
            $log = [
                'u_id' => $admin_info['id'],
                'url'  =>$url,
                'time' => time()
            ];
            Db::table("z_login_log")->insert($log);*/
            if($admin_info['is_admin'] != '1'){
                if(!in_array($url,$not_check)) {
                    $res = $auth->check($url, $admin_info['id']);
                    if (!$res) {
                        $this->error('你没有该操作权限','');
                    }
                }
            }
        }


    }
    /**
     * 首页内容数据
    */
    public function index_bak()
    {

        $node= new NodeModel();
        $node_menu = $node->getNodeList(['is_menu'=>'1']);
        $node_menu_tree = rule_to_tree($node_menu);
        $this->assign([
//            'menu' => json_encode($node_menu_tree)
            'menu' => $node_menu_tree
        ]);
//        dd($node_menu_tree);
        return $this->fetch("./index");
    }

    /**
     * 首页内容数据
     */
    public function index()
    {
        $menus = reD('node');
        $params = request()->param();
        if( !empty($params['id']) ){
            $pid = $params['id'];
        }else{
            $pid = 20;
        }

        $menu = $menus[$pid];
        $top_menu = [];
        foreach($menus as $v){
           if($v['pid'] == 0){
               $top_menu[] = $v;
           }
        }

//        dd($menus,$top_menu);
        $this->assign('pid',$pid);
        $this->assign('menus',$menus);
        $this->assign('menu',$menu);
        $this->assign('top_menu',$top_menu);
        return $this->fetch("./index");
    }

}